home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / ProcDoggie 1.0a6 / UEmergMem.p < prev    next >
Encoding:
Text File  |  1991-02-07  |  8.8 KB  |  209 lines  |  [TEXT/MPS ]

  1. UNIT UEmergMem;
  2.  
  3. {-------------------------------------------------------------------------------
  4. #
  5. #    Apple Macintosh Developer Technical Support
  6. #
  7. #    Interfaces for the emergency memory routines
  8. #
  9. #    Program:    ProcDoggie
  10. #    File:        UEmergMem.p - Pascal Implementation
  11. #
  12. #    by:        Forrest Tanaka
  13. #
  14. #    Copyright © 1988-1991 Apple Computer, Inc.
  15. #    All rights reserved.
  16. #
  17. --------------------------------------------------------------------------------
  18. #
  19. # EmergMem contains routines to handle emergency memory situations.  This is
  20. # used for Toolbox routines that either don’t check for memory-full errors, or
  21. # that call _SysErr when they can’t allocate the memory that they need.  The
  22. # purpose of the routines in this unit is to make sure that these toolbox
  23. # routines always get the memory they need.
  24. #
  25. -------------------------------------------------------------------------------}
  26. {[j=20/57/1$] Pasmat Options}
  27.  
  28.  
  29. INTERFACE
  30.  
  31.  
  32. (*******************************************************************************
  33. * Used Units
  34. *******************************************************************************)
  35.  
  36.     USES
  37.         (* Group 1 *)
  38.          Types
  39.         ,QuickDraw
  40.  
  41.         (* Group 2 *)
  42.         ,Memory
  43.         ,OSUtils;
  44.  
  45.  
  46. (*******************************************************************************
  47. * Constants
  48. *******************************************************************************)
  49.  
  50.     CONST
  51.         kAllocApp = TRUE; {For NewPtrMargin/NewHandleMargin for app heap alloc}
  52.         kAllocClr = TRUE; {For NewPtrMargin/NewHandleMargin to clear mem block}
  53.  
  54.  
  55. (*******************************************************************************
  56. * ConnectAppGZ - Connect the application grow zone proc
  57. *
  58. * This routine is called whenever this application’s simple grow-zone procedure
  59. * (see UEmergMem.inc1.p for the source for the grow-zone procedure) is to be
  60. * connected.  From this point on, any requests for memory by this application or
  61. * the system invoke our grow-zone procedure if there isn’t enough memory to
  62. * satisfy the request.
  63. *******************************************************************************)
  64.  
  65.     PROCEDURE ConnectAppGZ;
  66.  
  67.  
  68. (*******************************************************************************
  69. * DisconnectAppGZ - Disconnect the application grow zone proc
  70. *
  71. * This routine is called whenever this application’s simple grow-zone procedure
  72. * (see EmergMem.inc1.p for the source for the grow-zone procedure) is to be
  73. * disconnected.  From this point on, any requests for memory by this application
  74. * or the system return memFullErr if there isn’t enough memory to satisfy the
  75. * request.
  76. *******************************************************************************)
  77.  
  78.     PROCEDURE DisconnectAppGZ;
  79.  
  80.  
  81. (*******************************************************************************
  82. * InitEmergMem - Allocate emergency memory
  83. *
  84. * This is called at startup time to allocate the emergency memory block that’s
  85. * deallocated in the grow zone procedure (this application’s grow-zone procedure
  86. * is a privately-declared procedure defined in UEmergMem.inc1.p).  InitEmergMem
  87. * also installs this application’s grow-zone proc.
  88. *
  89. * If there isn’t enough memory to allocate the block of emergency memory, then
  90. * a subsequent call to FailLowMemory(0) returns TRUE.
  91. *******************************************************************************)
  92.  
  93.     PROCEDURE InitEmergMem;
  94.  
  95.  
  96. (*******************************************************************************
  97. * NoEmergMem - Check to see if emergency memory is being used or not
  98. *
  99. * Before my application attempts to use more memory, I call this routine to
  100. * check if I'm already using my emergency memory.  If so, then I’d better
  101. * prepare to die or get my emergency memory back.
  102. *******************************************************************************)
  103.  
  104.     FUNCTION NoEmergMem: Boolean;
  105.  
  106.  
  107. (*******************************************************************************
  108. * RecoverEmergMem - Recover emergency memory
  109. *
  110. * This is called from the event loop if NoEmergMem indicates that the emergency
  111. * memory was deallocated by this application’s grow-zone procedure.  This
  112. * routine will attempt recover the emergency memory.  If this fails, then some
  113. * usually some application options and commands are disabled until there is
  114. * enough free memory to enable them again.
  115. *******************************************************************************)
  116.  
  117.     PROCEDURE RecoverEmergMem;
  118.  
  119.  
  120. (*******************************************************************************
  121. * FailLowMemory - Is there enough free space in heap to allocate memory?
  122. *
  123. * FailLowMemory is called any time a potentially significant amount of non-
  124. * temporary memory is about to be allocated.  It returns TRUE if there’s enough
  125. * free space in the heap to allocate the requested amount of memory and still
  126. * have a significant amount of free space left over, and if the emergency memory
  127. * isn’t being used.  See UEmergMem.inc1.p for the definition of “significant
  128. * amount.”  "memRequest" specifies the number of bytes that are about to be
  129. * allocated.
  130. *
  131. * This routine is also used even if the amount of memory about to be allocated
  132. * isn’t clear.  In this case, it’s called after the significant amount of memory
  133. * is allocated and 0 is passed in memRequest.  If FailLowMemory returns TRUE,
  134. * then there’s was enough memory for the requested amount and still leave 32K
  135. * free and the emergency memory allocated.  If FailLowMemory returns FALSE, then
  136. * either there isn’t 32K free, or the emergency memory was deallocated by this
  137. * application’s grow-zone procedure, or both.  This is actually the usual way
  138. * that I use this function, because I normally use it for calls to the Toolbox,
  139. * and there’s usually no reliable way to determine how much memory the Toolbox
  140. * is going to allocate.
  141. *******************************************************************************)
  142.  
  143.     FUNCTION FailLowMemory (memRequest: LongInt): Boolean;
  144.  
  145.  
  146.  
  147. (*******************************************************************************
  148. * NewHandleMargin - Create a new handle without using emergency memory
  149. *
  150. * Many toolbox routines simply call SysErr when they run out of memory.  That’s
  151. * not too cool, so I try to make certain that the memory they need is always
  152. * available by making sure that I never request so much memory that the toolbox
  153. * routines are in danger of running out of memory and calling SysErr.  This is
  154. * achieved by calling NewHandleMargin instead of NewHandle any time a
  155. * relocatable memory block is desired.  NewHandle returns memFullErr in MemErr
  156. * if there isn’t enough free contiguous space to satisfy the request and still
  157. * leave a significant amount of free memory.
  158. *
  159. * NewHandleMargin returns NIL if there isn’t enough memory to allocated a block
  160. * of the size specified by "requestedSize".
  161. *
  162. * If "appHeapAlloc" is kAppHeap, then the block of memory is allocated in the
  163. * application’s heap.  If "appHeapAlloc" is NOT kAppHeap, then the block of
  164. * memory is allocated in the system heap.
  165. *
  166. * If "clearMem" is kAllocClr, then all the bytes in the block of memory are
  167. * cleared to zero.  If NOT kAllocClr is passed, then none of the bytes in the
  168. * block of memory are touched after being allocated.
  169. *******************************************************************************)
  170.  
  171.     FUNCTION NewHandleMargin (requestedSize: Size;
  172.                               appHeapAlloc:  Boolean;
  173.                               clearMem:      Boolean): Handle;
  174.  
  175.  
  176. (*******************************************************************************
  177. * NewPtrMargin - Create a new pointer without using emergency memory
  178. *
  179. * Many toolbox routines simply call SysErr when they run out of memory.  That’s
  180. * not too cool, so I try to make certain that the memory they need is always
  181. * available by making sure that I never request so much memory that the toolbox
  182. * routines are in danger of running out of memory and calling SysErr.  This is
  183. * achieved by calling NewPtr instead of NewHandle any time a non-relocatable
  184. * memory block is desired.  NewHandle returns memFullErr in MemErr if there
  185. * isn’t enough free contiguous space to satisfy the request and still leave a
  186. * significant amount of free memory.
  187. *
  188. * NewptrMargin returns NIL if there isn’t enough memory to allocated a block of
  189. * the size specified by "requestedSize".
  190. *
  191. * If "appHeapAlloc" is kAppHeap, then the block of memory is allocated in the
  192. * application’s heap.  If "appHeapAlloc" is NOT kAppHeap, then the block of
  193. * memory is allocated in the system heap.
  194. *
  195. * If "clearMem" is kAllocClr, then all the bytes in the block of memory are
  196. * cleared to zero.  If NOT kAllocClr is passed, then none of the bytes in the
  197. * block of memory are touched after being allocated.
  198. *******************************************************************************)
  199.  
  200.     FUNCTION NewPtrMargin (requestedSize: Size;
  201.                            appHeapAlloc:  Boolean;
  202.                            clearMem:      Boolean): Ptr;
  203.  
  204.  
  205. IMPLEMENTATION
  206.  
  207.     {$I UEmergMem.inc1.p}
  208.  
  209. END.